package com.nali.dal.statement.function;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Function {
	private String name;//name==null为字段或常量
	private List<FunctionArgument> argConfigs;
	private Function(String name, List<FunctionArgument> argConfigs) {
		this.name = name;
		this.argConfigs = argConfigs;
	}
	public static Function function(String name, List<FunctionArgument> argConfigs) {
		return new Function(name, argConfigs);
	}
	public static Function notFunction(FunctionArgument arg) {
		return new Function(null, Arrays.asList(arg));
	}
	public static Function column(String name) {
		return new Function(null, Arrays.asList(FunctionArgument.column(name)));
	}
	public static Function value(Object val) {
		return new Function(null, Arrays.asList(FunctionArgument.value(val)));
	}
	public String getName() {
		return name;
	}
	public List<FunctionArgument> getArgConfigs() {
		return argConfigs;
	}
	public List<Object> getArgs(Map<String,Object> dataMap) {
		List<Object> rlt = new ArrayList<Object>(argConfigs.size());
		for (FunctionArgument cfg : argConfigs) {
			rlt.add(cfg.getValue(dataMap));
		}
		return rlt;
	}
	public List<String> getColumns() {
		List<String> rlt = new ArrayList<String>(argConfigs.size());
		for (FunctionArgument cfg : argConfigs) {
			if(cfg.isColumn()) {
				rlt.add(cfg.getColumn());
			}
		}
		return rlt;
	}
	public boolean isFunction() {
		return name!=null;
	}
}
